package com.redlight.token.valid;

import com.redlight.base.BaseDao;
import com.redlight.base.CollectionUtil;
import com.redlight.base.SysUser;
import com.redlight.exception.AccountDeniedException;
import com.redlight.exception.InvalidCaptchaException;
import com.redlight.exception.InvalidPassException;
import com.redlight.exception.OauthTokenException;
import com.redlight.jdbc.JdbcParam;
import com.redlight.jdbc.JdbcUtil;
import com.redlight.token.Audience;
import com.redlight.token.LoginPara;
import com.redlight.token.security.Digests;
import com.redlight.utils.StringUtil;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;

/**
 * @author chao
 * @Title: DefaultOauthTokenValid
 * @date 2019-01-24 21:34
 */
public class DefaultOauthTokenValid extends OauthTokenValid {
    @Resource
    private BaseDao baseDao;
    protected final String defaultField = "id,password,enable,login_flag";

    public DefaultOauthTokenValid() {
    }


    @Override
    public SysUser valid(LoginPara loginPara) throws OauthTokenException {
        this.valid_captcha(loginPara);
        return this.valid_userPass(loginPara);
    }

    public void valid_captcha(LoginPara loginPara) throws InvalidCaptchaException {
        if (null != loginPara.getCaptchaCode()) {
            throw new InvalidCaptchaException();
        }
    }

    public SysUser valid_userPass(LoginPara loginPara) throws OauthTokenException {
        SysUser user = this.getUserByLoginName(loginPara.getLoginName());
//        user.setLoginFlag("admin");
//        user.setPassword("123456");
//        user.setEnable("1");
//        user.setLoginFlag("1");
        if (user == null) {
            throw new InvalidPassException();
        } else if (!"1".equals(user.getEnable())) {
            throw new AccountDeniedException();
        } else if (!"1".equals(user.getLoginFlag())) {
            throw new AccountDeniedException();
        } else {
            boolean digest = Digests.validatePassword(loginPara.getPassword(), user.getPassword());
            if (!digest) {
                throw new InvalidPassException();
            } else {
                return user;
            }
        }
    }
    @Override
    public SysUser getUserByLoginName(String loginName) {
        Audience audience = Audience.getInstance();
        String table = audience.getJdbcAuthnSearchTable();
        String paramField = audience.getJdbcAuthnSearchParamField();
        String field = audience.getJdbcAuthnSearchField();
        if (StringUtil.isNotBlank(table) && StringUtil.isNotBlank(paramField)) {
            if (StringUtil.isBlank(field)) {
                field = "id,password,enable,login_flag";
            }

            HashSet<String> fieldSet = new HashSet();
            String[] fieldArr = field.split(",");
            String[] dfArr = fieldArr;
            int var9 = fieldArr.length;

            int var10;
            for(var10 = 0; var10 < var9; ++var10) {
                String f = dfArr[var10];
                fieldSet.add(f.trim().toLowerCase());
            }

            if (!field.equals("id,password,enable,login_flag")) {
                dfArr = "id,password,enable,login_flag".split(",");
                String[] var15 = dfArr;
                var10 = dfArr.length;

                for(int var18 = 0; var18 < var10; ++var18) {
                    String f = var15[var18];
                    fieldSet.add(f.trim().toLowerCase());
                }
            }

            field = CollectionUtil.convertToString(fieldSet, ",");
            String sql = "select " + field + " from " + table + " where del_flag='0' and " + paramField + "=?";
            JdbcParam jdbcParam = new JdbcParam(loginName.toLowerCase(), String.class);
            List list = null;

            try {
                list = JdbcUtil.queryList(sql, jdbcParam, SysUser.class);
            } catch (Exception var13) {
                var13.printStackTrace();
            }

            if (null != list && 0 < list.size()) {
                return (SysUser)list.get(0);
            }
        }

        return null;
    }
    @Override
    public SysUser getUserById(String id) {
        Audience audience = Audience.getInstance();
        String table = audience.getJdbcAuthnSearchTable();
        String paramField = audience.getJdbcAuthnSearchParamField();
        String field = audience.getJdbcAuthnSearchField();
        if (StringUtil.isNotBlank(table) && StringUtil.isNotBlank(paramField)) {
            if (StringUtil.isBlank(field)) {
                field = "id,password,enable,login_flag";
            }

            HashSet<String> fieldSet = new HashSet();
            String[] fieldArr = field.split(",");
            String[] dfArr = fieldArr;
            int var9 = fieldArr.length;

            int var10;
            for(var10 = 0; var10 < var9; ++var10) {
                String f = dfArr[var10];
                fieldSet.add(f.trim().toLowerCase());
            }

            if (!field.equals("id,password,enable,login_flag")) {
                dfArr = "id,password,enable,login_flag".split(",");
                String[] var15 = dfArr;
                var10 = dfArr.length;

                for(int var18 = 0; var18 < var10; ++var18) {
                    String f = var15[var18];
                    fieldSet.add(f.trim().toLowerCase());
                }
            }

            field = CollectionUtil.convertToString(fieldSet, ",");
            String sql = "select " + field + " from " + table + " where del_flag='0' and id=?";
            JdbcParam jdbcParam = new JdbcParam(id, String.class);
            List list = null;

            try {
                list = JdbcUtil.queryList(sql, jdbcParam, SysUser.class);
            } catch (Exception var13) {
                var13.printStackTrace();
            }

            if (null != list && 0 < list.size()) {
                return (SysUser)list.get(0);
            }
        }

        return null;
    }
}
